package com.mldkhn.led_controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import com.larswerkman.colorpicker.ColorPicker;
import com.larswerkman.colorpicker.SaturationBar;
import com.larswerkman.colorpicker.ValueBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class Fragment_Flash extends Fragment implements OnSeekBarChangeListener, OnClickListener {
	private static Button btnFlashStart, btnFlashStop;
	private static ColorPicker picker;
	private static SaturationBar saturationBar;
	private static ValueBar valueBar;
	private static TextView lblFlashFrequency;
	private static SeekBar scrFlashFrequency;
	private static String strFlash = "FLASH";
	private static String strStop = "OFF";
	private String colorHEX;	
	private int progressFrequency = 0, color = 0;	
	
	private static int SERVERPORT;
	private static String SERVER_IP;
	private Socket socket;
	Thread socketThread;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	    Bundle savedInstanceState) {
	    View v = inflater.inflate(R.layout.fragment_flash, null);
	    picker = (ColorPicker) v.findViewById(R.id.colorPickerFlash);
	    saturationBar = (SaturationBar) v.findViewById(R.id.saturationBarFlash);
	    valueBar = (ValueBar) v.findViewById(R.id.valueBarFlash);
	    btnFlashStart = (Button) v.findViewById(R.id.btnFlashStart);
	    btnFlashStop = (Button) v.findViewById(R.id.btnFlashStop);
	    lblFlashFrequency = (TextView) v.findViewById(R.id.lblFlashFrequency);
	    scrFlashFrequency = (SeekBar) v.findViewById(R.id.scrFlashFrequency);
	    lblFlashFrequency.setText(" 0.0 Hz");
	    btnFlashStart.setOnClickListener(this);
	    btnFlashStop.setOnClickListener(this);
	    scrFlashFrequency.setOnSeekBarChangeListener(this);
	    picker.addSaturationBar(saturationBar);
	    picker.addValueBar(valueBar);
		try{
			SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
			SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
			SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
			socketThread = new Thread(new ClientThread());
			socketThread.start();
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())),
					true);
			out.println("HELLO");
			out.close();
			socketThread.interrupt();
			socketThread = null;
			
			} 
			  catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
	    return v;
		}

	@SuppressLint("DefaultLocale")
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnFlashStart:
			color = picker.getColor();
			colorHEX = Integer.toHexString(color);
			picker.setOldCenterColor(picker.getColor());
			try{
				SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
				SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
				SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
				socketThread = new Thread(new ClientThread());
				socketThread.start();
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())),
						true);
				String f;
				f = String.format("%03d", progressFrequency);
				out.println(strFlash + "\n" + colorHEX + "\n" + f);
				out.close();
				socketThread.interrupt();
				socketThread = null;
				
				} 
				  catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}

            break;
        case R.id.btnFlashStop:
			try{
				SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
				SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
				SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
				socketThread = new Thread(new ClientThread());
				socketThread.start();
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())),
						true);
				out.println(strStop);
				socketThread.interrupt();
				socketThread = null;
				
				} 
				  catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
        	
		}
		
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		switch(seekBar.getId()){
		case R.id.scrFlashFrequency:
			lblFlashFrequency.setText(" " + (float)progress/10 + " Hz");
			progressFrequency = progress;
			break;
		}
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar arg0) {
		// TODO Auto-generated method stub
		
	}
	
	class ClientThread implements Runnable {
		
		@Override
		public void run() {
			
			try {
				InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

				socket = new Socket(serverAddr, SERVERPORT);

			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
